#include <string.h>
#include "stm32f10x.h"
#include "../SYSTEM/utils/utils.h"
#include "../SYSTEM/spi/spi.h"
#include "../SYSTEM/usart/usart.h"
#include "../SYSTEM/utils/utils.h"
// #include "../SYSTEM/bl/bl0939.h"
// #include "../SYSTEM/flash/flash.h"
#include "../SYSTEM/timer/timer.h"
#include "../SYSTEM/flash/flash.h"
#include "./dlt645/dlt645.h"
#include "../SYSTEM/bl/BL6552.h"

u8 USART2_Data[256];	   // 串口2 plc 接收缓存
u8 indexs = 0;			   // 串口2接收的位置
uint16_t last_count = 0;   // 上次COUNT值
uint16_t last_EnergyP = 0; // 上次度数值
Eng_Cal_StructDef Eng_Cal; // 电能计算的中间变量，用于参数传递
Coef_StructDef BL6552_Coef;
Elect_StructDef BL6552_Elect;
Eng_CFCnt_StructDef Eng_CFCnt; // 小于1度电的脉冲底数

/**
 *@file 主文件
 */
void Delay(u32 count)
{
	u32 i = 0;
	for (; i < count; i++)
		;
}

int main(void)
{
	// u32 in_data[5] = {0x11, 0x22, 0x33, 0x44, 0x55};
	// u32 out_data[5] = {0};
	// u8 STATUS = 0;
	// u8 i;
	// Delay(1000);
	Usart1_Init(9600); // print com
	SPrint("##starting boot##\r\n");
	Usart2_Init(9600); // plc com
	// uint8_t FlashTest[] = "wisdomopen";

	// bl9390 chipset initial
	// if (BL_Init() == 1)
	// bl 6552 三项
	if (BL_Init_6552() == 1)
	{
		SPrint("##BL chipset initial success##\r\n");
	}
	else
	{
		return 0;
	}
	timer_init();
	// 读停电记录数量
	stop_get_inital_value();

	// FLASH write test
	// STATUS = WriteFlash(0x0800C800, in_data, 5);
	// if (1 == STATUS)
	// {
	// 	SPrint("write ok...\r\n");
	// 	ReadFlash(0x0800C800, out_data, 5);
	// 	SPrint("\r\n The Five Data Is : \r\n");
	// 	for (i = 0; i < 5; i++)
	// 	{
	// 		WPrint("\r %d \r", out_data[i]);
	// 	}
	// }
	// FlashWriteBuff( DEVICE_INFO_ADDRESS, FlashTest,sizeof(FlashTest) );
	// return 0;
	SPrint("wisdomopen infomation...\r\n");
	while (1)
	{
		// SPrint("wisdomopen infomation...\r\n");
		// Delay(1000000);
	}
}

// TIM2中断处理 1 秒定时器
void TIM2_IRQHandler()
{
	// Elect_StructDef data;
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

		// data = BL0939_ReadEData();
		if (BL65XX_OK == BL6552_Elect_Proc())
		{
			WPrint("IA IB IC : %d, %d, %d \r\n",BL6552_Elect.IA_RMS,BL6552_Elect.IB_RMS,BL6552_Elect.IC_RMS);
			// WPrint("VA VB VC : %d, %d, %d \r\n",BL6552_Elect.VA_RMS,BL6552_Elect.VB_RMS,BL6552_Elect.VC_RMS);
			if (BL6552_Elect.IA_RMS < 5 && BL6552_Elect.IB_RMS < 5 && BL6552_Elect.IC_RMS < 5)
			{
				SPrint("###stop...\r\n");
				stop_process();
				return;
			}
			else
			{
				stop_restore_record();
			}
			// SPrint("###get data ok...\r\n");
			// WPrint("IA IB IC : %d, %d, %d \r\n",BL6552_Elect.IA_RMS,BL6552_Elect.IB_RMS,BL6552_Elect.IC_RMS);
			
			// WPrint("WA WB WC : %d, %d, %d \r\n",BL6552_Elect.WATT_A,BL6552_Elect.WATT_B,BL6552_Elect.WATT_C);
			// WPrint("pA pB pC : %d, %d, %d \r\n",BL6552_Elect.EnergyP_A,BL6552_Elect.EnergyP_B,BL6552_Elect.EnergyP_C);
			// WPrint("engery: %d,COUNT:%d\r\n", BL65XX_GetTotalEnergy(),Eng_CFCnt.CNT);
		}
		else
		{
			SPrint("###get data failure...\r\n");
		}

		// SPrint("read...\r\n");
		// WPrint("read data Current_Value_A: %.2f \r\n", 1.0 * data.Current_Value_A / 100);
		// WPrint("read data Voltage_Value: %d \r\n", data.Voltage_Value);
		// WPrint("read data Power_Value_A: %d \r\n", data.Power_Value_A);
		// WPrint("read data Energy_kwh_A: %d \r\n", data.Energy_kwh_A);
		// WPrint("read data Mid_CFA_CNT: %d \r\n", data.Mid_CFA_CNT);
	}
}

// USART2 中断处理函数
void USART2_IRQHandler(void)
{

	if (RESET != USART_GetITStatus(USART2, USART_IT_RXNE))
	{
		// SPrint("*uart irq \r\n");

		// USART_ClearITPendingBit(USART2,USART_IT_RXNE);
		USART2_Data[indexs] = USART_ReceiveData(USART2);
		indexs++;
		if (indexs > 255)
		{
			SPrint("Error: message too long! \r\n");
			indexs = 0;
		}
	}

	if (RESET != USART_GetITStatus(USART2, USART_IT_IDLE))
	{ // 空闲总线中断
		// return;
		if (indexs == 0)
		{
			USART_ReceiveData(USART2); // 清除空闲总线标志位
			return;
		}
		WPrint("receiver packet ,len = %d  \r\n", indexs);
		dlt645_print_packet(USART2_Data, indexs);
		dlt645_receive_msg(USART2_Data, indexs);
		indexs = 0;
		memset(USART2_Data, 0, sizeof(USART2_Data));
		USART_ReceiveData(USART2); // 清除空闲总线标志位
		return;
	}
}
